package com.primb.bench.project.home.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.primb.baselibs.view.AutoToolbar;
import com.primb.baselibs.view.RecycleViewDivider;
import com.primb.bench.R;
import com.primb.bench.base.SwipeBaseActivity;
import com.primb.bench.common.constant.AppConstant;

import java.util.ArrayList;

import butterknife.BindView;

import static android.R.attr.type;

/**
 * Created by Chen on 2017/5/8.
 * 功能描述：
 */

public class InfoListActivity extends SwipeBaseActivity {

    @BindView(R.id.title_bar)
    TextView titleBar;
    @BindView(R.id.tool_bar)
    AutoToolbar toolBar;
    @BindView(R.id.recycler_add_info)
    RecyclerView recyclerAddInfo;
    @BindView(R.id.tv_top)
    TextView tvTop;
    private ArrayList<String> dataList;
    private BaseQuickAdapter<String, BaseViewHolder> quickAdapter;
    private int code;
    private String listStr;

    @Override
    protected void onBaseIntent() {
        super.onBaseIntent();
        Intent intent = getIntent();
        code = intent.getIntExtra("code", -1);
    }

    @Override
    protected int onBaseLayoutId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_list;
    }

    @Override
    protected void onBaseBindView() {
        setActionBarForType(code);

        titleBar.setText("选择" + listStr);
        toolBar.setNavigationOnClickListener(v -> InfoListActivity.this.finish());
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerAddInfo.setLayoutManager(layoutManager);
        recyclerAddInfo.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL));
        recyclerAddInfo.setItemAnimator(new DefaultItemAnimator());
        dataList = new ArrayList<>();
        recyclerAddInfo.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));

        quickAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_info_list, dataList) {

            @Override
            protected void convert(BaseViewHolder helper, String item) {
                helper.setText(R.id.tv_item_info, item);
            }
        };

        quickAdapter.setOnItemClickListener((adapter, view, position) -> {
            String info = dataList.get(position);
            setResultForType(code, info);
        });
        View layoutEmpty = getLayoutInflater().inflate(R.layout.empty_list_view, (ViewGroup) recyclerAddInfo.getParent(), false);
        quickAdapter.setEmptyView(layoutEmpty);
        quickAdapter.isFirstOnly(false);
        quickAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        quickAdapter.setNotDoAnimationCount(0);
        recyclerAddInfo.setAdapter(quickAdapter);
    }

    private void setActionBarForType(int code) {
        switch (code) {
            case AppConstant.ADD_CUSTOMER_TYPE:
                listStr = "客户类型";
                break;
            case AppConstant.ADD_CUSTOMER_CREDIT:
                listStr = "客户信用";
                break;
            case AppConstant.ADD_CUSTOMER_SIZE:
                listStr = "客户规模";
                break;
            case AppConstant.ADD_LOAN_TYPE:
                listStr = "币种";
                break;
            case AppConstant.ADD_LOAN_DATE:
                listStr = "贷款期限";
                break;
            case AppConstant.ADD_LOAN_BUSINESS_TYPE:
                listStr = "贷款信贷品种";
                break;
            case AppConstant.ADD_LOAN_INDUSTRY:
                listStr = "贷款投入行业";
                break;
            case AppConstant.ADD_LOAN_RATE_TYPE:
                listStr = "贷款利率类型";
                break;
            case AppConstant.ADD_ENSURE_MAIN_TYPE:
                listStr = "主担保方式";
                break;
            case AppConstant.ADD_ENSURE_CONCRETENESS:
                listStr = "担保具体类型";
                break;
            case AppConstant.ADD_ENSURE_OBJECT_TYPE:
                listStr = "质押品类型";
                break;
        }
    }


    private void setResultForType(int code, String info) {
        Intent intent = new Intent();
        switch (code) {
            case AppConstant.ADD_CUSTOMER_TYPE:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_CUSTOMER_TYPE, intent);
                this.finish();
                break;
            case AppConstant.ADD_CUSTOMER_CREDIT:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_CUSTOMER_CREDIT, intent);
                this.finish();
                break;
            case AppConstant.ADD_CUSTOMER_SIZE:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_CUSTOMER_SIZE, intent);
                this.finish();
                break;
            case AppConstant.ADD_LOAN_TYPE:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_LOAN_TYPE, intent);
                this.finish();
                break;
            case AppConstant.ADD_LOAN_DATE:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_LOAN_DATE, intent);
                this.finish();
                break;
            case AppConstant.ADD_LOAN_BUSINESS_TYPE:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_LOAN_BUSINESS_TYPE, intent);
                this.finish();
                break;
            case AppConstant.ADD_LOAN_INDUSTRY:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_LOAN_INDUSTRY, intent);
                this.finish();
                break;
            case AppConstant.ADD_LOAN_RATE_TYPE:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_LOAN_RATE_TYPE, intent);
                this.finish();
                break;
            case AppConstant.ADD_ENSURE_MAIN_TYPE:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_ENSURE_MAIN_TYPE, intent);
                this.finish();
                break;
            case AppConstant.ADD_ENSURE_CONCRETENESS:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_ENSURE_CONCRETENESS, intent);
                this.finish();
                break;
            case AppConstant.ADD_ENSURE_OBJECT_TYPE:
                intent.putExtra("info", info);
                this.setResult(AppConstant.ADD_ENSURE_OBJECT_RESULT, intent);
                this.finish();
                break;
        }
    }

    @Override
    protected void onLoadData() {
        if (AppConstant.ADD_ENSURE_MAIN_TYPE == code) {
            dataList.add("保证");
            dataList.add("信用");
            dataList.add("抵押");
            dataList.add("质押");
            return;
        }
        for (int i = 0; i < 10; i++) {
            dataList.add(listStr + i);
        }
    }
}
